package com.xinzhi.utils;


import com.tencentcloudapi.common.Credential;
import com.tencentcloudapi.common.exception.TencentCloudSDKException;
import com.tencentcloudapi.common.profile.ClientProfile;
import com.tencentcloudapi.common.profile.HttpProfile;
import com.tencentcloudapi.sms.v20210111.SmsClient;
import com.tencentcloudapi.sms.v20210111.models.SendSmsRequest;
import com.tencentcloudapi.sms.v20210111.models.SendSmsResponse;
import org.springframework.stereotype.Component;

import java.util.Random;
@Component
public class TengXunSMSUtils {
//    public static void main(String[] args) {
//        try {
//            String templateID = "2193808";
//            String phoneNumbers = "19935445399";
//            // 随机生成一个6位数的验证码
//            String strNumber = generateValidateCode(6).toString();
//            // 验证码有效时间，单位为分钟
//            String validityPeriod = "1";
//            sendShortMessage(templateID, phoneNumbers, strNumber, validityPeriod);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

    /**
     * 随机生成验证码
     *
     * @param length 长度为4位或者6位
     * @return
     */
    public static Integer generateValidateCode(Integer length) {
        Integer code = null;
        Random random = new Random();
        if (length == 4) {
            code = random.nextInt(8999) + 1000; // 生成 1000 到 9999 之间的随机数

        } else if (length == 6) {
            code = random.nextInt(899999) + 100000; // 生成 100000 到 999999 之间的随机数
        } else {
            throw new RuntimeException("只能生成4位或6位数字验证码");
        }
        return code;
    }

    public static void sendShortMessage(String templateCode, String phoneNum, String param1, String param2) {
        try {
            // 从环境变量读取密钥
            String secretId = "AKIDfGh4X6Y6Zu9GWgmiYKCBLqEAILH6tcTO";
            String secretKey = "8OYdsImeR8qSB61qkmsxzvocSA1v040r";

            if (secretId == null || secretKey == null) {
                throw new RuntimeException("环境变量 TENCENT_CLOUD_SECRET_ID 或 TENCENT_CLOUD_SECRET_KEY 未设置");
            }

            Credential cred = new Credential(secretId, secretKey);

            HttpProfile httpProfile = new HttpProfile();
            httpProfile.setReqMethod("POST");
            httpProfile.setConnTimeout(60);
            httpProfile.setEndpoint("sms.tencentcloudapi.com");

            ClientProfile clientProfile = new ClientProfile();
            clientProfile.setSignMethod("HmacSHA256");
            clientProfile.setHttpProfile(httpProfile);

            SmsClient client = new SmsClient(cred, "ap-guangzhou", clientProfile);

            SendSmsRequest req = new SendSmsRequest();
            req.setSmsSdkAppId("1400919828");
            req.setSignName("66ccffIcu网");
            req.setSenderId("");
            req.setTemplateId(templateCode);
            req.setPhoneNumberSet(new String[]{"+86" + phoneNum});
            req.setTemplateParamSet(new String[]{param1, param2});

            SendSmsResponse res = client.SendSms(req);

            // 输出 JSON 格式的字符串回包
            System.out.println(SendSmsResponse.toJsonString(res));
            // 打印请求 ID
            System.out.println("RequestId: " + res.getRequestId());

        } catch (TencentCloudSDKException e) {
            System.err.println("腾讯云 SDK 异常: " + e.getMessage());
            e.printStackTrace();
        } catch (Exception e) {
            System.err.println("通用异常: " + e.getMessage());
            e.printStackTrace();
        }
    }
}